import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Text Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const TextDemoPage(),
    );
  }
}

class TextDemoPage extends StatefulWidget {
  const TextDemoPage({super.key});

  @override
  State<TextDemoPage> createState() => _TextDemoPageState();
}

class _TextDemoPageState extends State<TextDemoPage> {
  late TapGestureRecognizer _tapRecognizer;

  @override
  void initState() {
    super.initState();
    _tapRecognizer =
        TapGestureRecognizer()
          ..onTap = () {
            debugPrint('点击了链接');
          };
  }

  @override
  void dispose() {
    _tapRecognizer.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('Text 组件演示'),
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            buildTextDemo(),
            const SizedBox(height: 20),
            buildTextStyleDemo(),
            const SizedBox(height: 20),
            buildTextSpanDemo(),
            const SizedBox(height: 20),
            buildDefaultTextStyleDemo(),
            const SizedBox(height: 20),
            buildCustomFontDemo(),
          ],
        ),
      ),
    );
  }

  Widget buildTextDemo() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          '基本 Text 组件:',
          style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
        ),
        const SizedBox(height: 10),
        const Text('这是一个普通的文本'),
        const Text(
          '这是一个带有最大行数限制的长文本，当文本超过指定行数时会被截断并显示省略号。这里设置了最大2行。',
          maxLines: 2,
          overflow: TextOverflow.ellipsis,
        ),
        const Text('这是一个居中对齐的文本', textAlign: TextAlign.center),
      ],
    );
  }

  Widget buildTextStyleDemo() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          'TextStyle 样式演示:',
          style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
        ),
        const SizedBox(height: 10),
        const Text(
          '红色粗体文本',
          style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold),
        ),
        const Text(
          '大号斜体文本',
          style: TextStyle(fontSize: 24, fontStyle: FontStyle.italic),
        ),
        const Text(
          '带下划线的文本',
          style: TextStyle(
            decoration: TextDecoration.underline,
            decorationColor: Colors.blue,
          ),
        ),
        const Text(
          '带删除线的文本',
          style: TextStyle(
            decoration: TextDecoration.lineThrough,
            decorationColor: Colors.red,
          ),
        ),
      ],
    );
  }

  Widget buildTextSpanDemo() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          'TextSpan 富文本演示:',
          style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
        ),
        const SizedBox(height: 10),
        Text.rich(
          TextSpan(
            text: '这是一个富文本示例，包含',
            style: const TextStyle(fontSize: 16),
            children: [
              const TextSpan(
                text: '红色粗体',
                style: TextStyle(
                  color: Colors.red,
                  fontWeight: FontWeight.bold,
                ),
              ),
              const TextSpan(text: '和'),
              const TextSpan(
                text: '蓝色斜体',
                style: TextStyle(
                  color: Colors.blue,
                  fontStyle: FontStyle.italic,
                ),
              ),
              const TextSpan(text: '以及'),
              TextSpan(
                text: '可点击的链接',
                style: const TextStyle(
                  color: Colors.blue,
                  decoration: TextDecoration.underline,
                ),
                recognizer: _tapRecognizer,
              ),
              const TextSpan(text: '。'),
            ],
          ),
        ),
      ],
    );
  }

  Widget buildDefaultTextStyleDemo() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          'DefaultTextStyle 演示:',
          style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
        ),
        const SizedBox(height: 10),
        DefaultTextStyle(
          style: const TextStyle(
            color: Colors.green,
            fontSize: 18,
            fontWeight: FontWeight.w500,
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Text('这个文本继承了默认样式'),
              const Text('这个也是'),
              const Text('这个文本覆盖了颜色', style: TextStyle(color: Colors.purple)),
            ],
          ),
        ),
      ],
    );
  }

  Widget buildCustomFontDemo() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          '字体演示:',
          style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
        ),
        const SizedBox(height: 10),
        const Text('默认字体文本', style: TextStyle(fontSize: 16)),
        const Text(
          'Monospace 等宽字体',
          style: TextStyle(fontSize: 16, fontFamily: 'monospace'),
        ),
        const Text(
          'Serif 衬线字体',
          style: TextStyle(fontSize: 16, fontFamily: 'serif'),
        ),
      ],
    );
  }
}
